package com.pjedu.ssmdemo.controller;


import com.pjedu.ssmdemo.entity.User;
import com.pjedu.ssmdemo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author 汪洋
 * @Description
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/findAll.user")
    @ResponseBody
    public Object findAll(){
        //拿到所有的数据
        List<User> users = userService.findAll();
        return users;
    }
    @RequestMapping("/findById.user")
    @ResponseBody
    public Object findById(@RequestParam("id") String id){
        //查询该id对应的User对象
        User user = userService.findById(Integer.parseInt(id));
        return user;
    }
    @ResponseBody
    @PostMapping("/updateUser.user")
    public String update(@RequestBody User user){
        userService.updateUser(user);
        return "success";
    }
    @ResponseBody
    @PostMapping("/login.user")
    public String login(@RequestBody User user, HttpSession session){
        if (userService.login(user)!=null){
            session.setAttribute("user",user);
            return "success";
        }
        return "fail";
    }
}
